package com.ruoyi.crm.activity.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.crm.activity.mapper.CrmActivityMapper;
import com.ruoyi.crm.activity.domain.CrmActivity;
import com.ruoyi.crm.activity.service.ICrmActivityService;
import com.ruoyi.crm.sale.domain.CrmBusiness;
import com.ruoyi.crm.sale.domain.CrmClue;
import com.ruoyi.crm.sale.domain.CrmContract;
import com.ruoyi.crm.sale.domain.CrmCustomer;
import com.ruoyi.crm.sale.mapper.CrmBusinessMapper;
import com.ruoyi.crm.sale.mapper.CrmClueMapper;
import com.ruoyi.crm.sale.mapper.CrmContractMapper;
import com.ruoyi.crm.sale.mapper.CrmCustomerMapper;

/**
 * crm活动表Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-09-06
 */
@Service
public class CrmActivityServiceImpl implements ICrmActivityService 
{
    @Autowired
    private CrmActivityMapper crmActivityMapper;
    @Autowired
    private CrmClueMapper crmClueMapper;
    @Autowired
    private CrmCustomerMapper crmCustomerMapper;
    @Autowired
    private CrmBusinessMapper crmBusinessMapper;
    @Autowired
    private CrmContractMapper crmContractMapper;

    /**
     * 查询crm活动表
     * 
     * @param id crm活动表ID
     * @return crm活动表
     */
    @Override
    public CrmActivity selectCrmActivityById(Long id)
    {
        return crmActivityMapper.selectCrmActivityById(id);
    }

    /**
     * 查询crm活动表列表
     * 
     * @param crmActivity crm活动表
     * @return crm活动表
     */
    @Override
    public List<CrmActivity> selectCrmActivityList(CrmActivity crmActivity)
    {
        return crmActivityMapper.selectCrmActivityList(crmActivity);
    }

    /**
     * 新增crm活动表
     * 
     * @param crmActivity crm活动表
     * @return 结果
     */
    @Override
    public int insertCrmActivity(CrmActivity crmActivity)
    {
        crmActivity.setCreateTime(DateUtils.getNowDate());
        crmActivity.setUpdateTime(DateUtils.getNowDate());
        crmActivity.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
        
        
        if(1 == crmActivity.getActivityType()) {//线索
        	//更新下次联系时间
            CrmClue clue = crmClueMapper.selectCrmClueById(crmActivity.getBusinessId());
            clue.setNextTime(crmActivity.getNextTime());
            clue.setLastTime(crmActivity.getNextTime());
            clue.setLastCon(crmActivity.getContent());
            crmClueMapper.updateCrmClue(clue);
        }else if(2 == crmActivity.getActivityType()){//客户
        	CrmCustomer customer = crmCustomerMapper.selectCrmCustomerById(crmActivity.getBusinessId());
        	customer.setNextTime(crmActivity.getNextTime());
        	customer.setLastTime(crmActivity.getNextTime());
        	customer.setLastCon(crmActivity.getContent());
        	crmCustomerMapper.updateCrmCustomer(customer);
        }else if(3 == crmActivity.getActivityType()){//商机
        	CrmBusiness bus = crmBusinessMapper.selectCrmBusinessById(crmActivity.getBusinessId());
        	bus.setNextTime(crmActivity.getNextTime());
        	bus.setLastTime(crmActivity.getNextTime());
        	bus.setLastCon(crmActivity.getContent());
        	crmBusinessMapper.updateCrmBusiness(bus);
        }else if(6 == crmActivity.getActivityType()){//合同
        	CrmContract contract = crmContractMapper.selectCrmContractById(crmActivity.getBusinessId());
//        	contract.setNextTime(crmActivity.getNextTime());
        	contract.setLastTime(crmActivity.getNextTime());
//        	contract.setLastCon(crmActivity.getContent());
        	crmContractMapper.updateCrmContract(contract);
        }
        
        
        return crmActivityMapper.insertCrmActivity(crmActivity);
    }

    /**
     * 修改crm活动表
     * 
     * @param crmActivity crm活动表
     * @return 结果
     */
    @Override
    public int updateCrmActivity(CrmActivity crmActivity)
    {
        crmActivity.setUpdateTime(DateUtils.getNowDate());
        crmActivity.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
        
        if(1 == crmActivity.getStatus() && 1 == crmActivity.getActivityType()) {//线索
        	//更新线索表最后跟进时间、最后跟进记录
            CrmClue clue = crmClueMapper.selectCrmClueById(crmActivity.getBusinessId());
            clue.setLastTime(crmActivity.getNextTime());
            clue.setLastCon(crmActivity.getContent());
            crmClueMapper.updateCrmClue(clue);
        }else if(1 == crmActivity.getStatus() && 2 == crmActivity.getActivityType() && 1 == crmActivity.getType()){//客户
        	CrmCustomer customer = crmCustomerMapper.selectCrmCustomerById(crmActivity.getBusinessId());
        	customer.setLastTime(crmActivity.getNextTime());
        	customer.setLastCon(crmActivity.getContent());
        	crmCustomerMapper.updateCrmCustomer(customer);
        }else if(1 == crmActivity.getStatus() && 3 == crmActivity.getActivityType() && 1 == crmActivity.getType()){//商机
        	CrmBusiness bus = crmBusinessMapper.selectCrmBusinessById(crmActivity.getBusinessId());
        	bus.setLastTime(crmActivity.getNextTime());
        	bus.setLastCon(crmActivity.getContent());
        	crmBusinessMapper.updateCrmBusiness(bus);
        }
        
        return crmActivityMapper.updateCrmActivity(crmActivity);
    }

    /**
     * 批量删除crm活动表
     * 
     * @param ids 需要删除的crm活动表ID
     * @return 结果
     */
    @Override
    public int deleteCrmActivityByIds(Long[] ids)
    {
        return crmActivityMapper.deleteCrmActivityByIds(ids);
    }

    /**
     * 删除crm活动表信息
     * 
     * @param id crm活动表ID
     * @return 结果
     */
    @Override
    public int deleteCrmActivityById(Long id)
    {
        return crmActivityMapper.deleteCrmActivityById(id);
    }

    /**
     * 快到期提醒
     * 
     * @return crm活动表集合
     */
    public List<Map<String, Object>> selDealtWith(){
    	return crmActivityMapper.selDealtWith();
    }
}
